package org.example.B_100;

import java.beans.Introspector;
import java.util.Scanner;

/**
 * 代码编辑器
 */
public class CodeEditor {
    public static void main(String[] args) {
        //输入行数K
        Scanner sc = new Scanner(System.in);
        int k = Integer.parseInt(sc.nextLine());

        //输入原始字符串
        StringBuilder s = new StringBuilder(sc.nextLine());


        String[][] commands = new String[k][2];

        //输入多行命令（指令，值）
        for (int i = 0; i < k; i++) {
            //逐行遍历将输入分组存储
            commands[i] = sc.nextLine().split(" ");
        }
        //输出结果
        System.out.println(getResult(s,commands));

    }

    //功能执行过程
    public static String getResult(StringBuilder s,String[][] commands){
        //定义光标位置
        int curIdx = 0;

        //遍历每一个命令
        for(String[] command : commands){
            switch (command[0]){
                case "FORWARD":
                    curIdx += Integer.parseInt(command[1]);
                    curIdx = Math.min(curIdx,s.length());
                    break;
                case "BACKWARD":
                    curIdx -= Integer.parseInt(command[1]);
                    curIdx = Math.max(curIdx,0);
                    break;
                case "SEARCH-FORWARD":
                    int i = s.indexOf(command[1],curIdx);
                    if (i != -1) curIdx = i;
                    break;
                case "SEARCH-BACKWARD":
                    int j = s.lastIndexOf(command[1],curIdx);
                    if (j != -1) curIdx = j;
                    break;
                case "INSERT":
                    s.insert(curIdx,command[1]);
                    curIdx += command[1].length();
                    break;
                case "REPLACE":
                    s.replace(curIdx,curIdx+command[1].length(),command[1]);
                    break;
                case "DELETE":
                    s.delete(curIdx,curIdx+Integer.parseInt(command[1]));
                    break;
            }
        }
        return s.toString();
    }

}
